﻿using System;
using System.Timers;
public class AlarmEventArgs : EventArgs
{
    public DateTime CurrentTime { get; set; }
    public AlarmEventArgs(DateTime currentTime)
    {
        CurrentTime = currentTime;
    }
}
public class AlarmClock
{
    public event EventHandler<AlarmEventArgs> Tick;
    public event EventHandler<AlarmEventArgs> Alarm;

    private System.Timers.Timer timer; // 定时器
    private DateTime alarmTime; // 响铃时间
    public AlarmClock(DateTime alarmTime)
    {
        this.alarmTime = alarmTime;
        timer = new System.Timers.Timer(1000); // 初始化定时器，每秒触发一次（1000 毫秒 = 1 秒）
        timer.Elapsed += OnTimerElapsed;
    }

    public void Start()
    {
        timer.Start();
    }
    public void Stop()
    {
        timer.Stop();
    }

    private void OnTimerElapsed(object sender, ElapsedEventArgs e)
    {
        DateTime currentTime = DateTime.Now;

 
        Tick?.Invoke(this, new AlarmEventArgs(currentTime));
        if (currentTime >= alarmTime)
        {
            Alarm?.Invoke(this, new AlarmEventArgs(currentTime));
            Stop(); 
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        DateTime alarmTime = DateTime.Now.AddSeconds(10);

        AlarmClock alarmClock = new AlarmClock(alarmTime);

        alarmClock.Tick += (sender, e) =>
        {
            Console.WriteLine($"嘀嗒: 当前时间 {e.CurrentTime:HH:mm:ss}");
        };

        alarmClock.Alarm += (sender, e) =>
        {
            Console.WriteLine($"响铃: 当前时间 {e.CurrentTime:HH:mm:ss}");
        };

        Console.WriteLine($"闹钟已启动，响铃时间为 {alarmTime:HH:mm:ss}");
        alarmClock.Start();

        Console.ReadLine();
    }
}